#!/usr/bin/python -S
"""
testdata.py
"""

__author__ = 'Andy Chu'


import codecs
import os
import sys
try:
  import json
except ImportError:
  import simplejson as json


class Error(Exception):
  pass


class DiskDataLoader(object):
  """Put more methods here to do various kinds of conversions."""

  def __init__(self, base_dir):
    self.base_dir = base_dir

  def Bytes(self, rel_path):
    path = os.path.join(self.base_dir, rel_path)
    f = open(path, 'rb')  # for bytes, use binary mode
    contents = f.read()
    f.close()
    return contents

  def String(self, rel_path, encoding='utf-8'):
    """Returns a unicode string."""
    path = os.path.join(self.base_dir, rel_path)
    f = codecs.open(path, 'r', encoding=encoding)
    contents = f.read()
    f.close()
    return contents

  def DecodeJson(self, rel_path):
    s = self.String(rel_path)
    return json.loads(s)
